<template>
  <div id="app">
    <el-card class="login-card">
      <el-form :model="ruleForm" :rules="rules" ref="test" style="margin-top: 30px">
        <el-form-item prop="mobile">
          <el-input v-model="ruleForm.mobile" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="ruleForm.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
           <el-button type="primary" style="width: 100%;" @click="submitForm('test')">登录</el-button>
        </el-form-item>
        <el-form-item>
           <el-button style="width: 100%;" @click="resetForm('test')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'App',
  data () {
    const validateMobile = (rule, value, callback) => {
      if (value[2] !== '8') {
        callback(new Error('第三位手机号是 8'))
      } else {
        callback()
      }
    }

    return {
      ruleForm: {
        mobile: '',
        password: ''
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确手机号', trigger: 'blur' },
          { validator: validateMobile, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12,message: '请输入 6 - 12 位密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          alert('submit!')
        } else {
          console.log('error submit111111!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style>
body {
  margin: 0;
}

#app {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100vh;
  background-color: pink;
}

.login-card {
  width: 440px;
  height: 300px;
}
</style>